<% content_for :page_title do %>
  <%= plural_resource_name(Spree::Product) %>
<% end %>

<% content_for :page_actions do %>
  <%= button_link_to Spree.t(:new_product), new_object_url, { class: "btn-success", icon: 'add.svg', id: 'admin_new_product' } %>
<% end if can?(:create, Spree::Product) %>

<% content_for :table_filter do %>
  <div data-hook="admin_products_sidebar">

    <%= search_form_for [:admin, @search] do |f| %>
      <%- locals = {f: f} %>
      <div data-hook="admin_products_index_search" class="row">
        <div class="col-12 col-lg-6">
          <div class="form-group">
            <%= f.label :search_by_name, Spree.t(:name) %>
            <%= f.text_field :search_by_name, size: 15, class: "form-control js-quick-search-target js-filterable" %>
          </div>
        </div>
        <div class="col-12 col-lg-6">
          <div class="form-group">
            <%= f.label :variants_including_master_sku_cont, Spree.t(:sku) %>
            <%= f.text_field :variants_including_master_sku_cont, size: 15, class: "form-control js-filterable" %>
          </div>
        </div>
        <div class="col-12">
          <div class="field checkbox mb-2">
            <label>
              <%= f.check_box :deleted_at_null, {checked: params[:q][:deleted_at_null] == '0'}, '0', '1' %>
              <%= Spree.t(:show_deleted) %>
            </label>
          </div>

          <div class="field checkbox mb-2">
            <label>
              <%= f.check_box :not_discontinued, {checked: params[:q][:not_discontinued] == '0'}, '0', '1' %>
              <%= Spree.t(:show_discontinued ) %>
            </label>
          </div>
        </div>
      </div>
      <div data-hook="admin_products_index_search_buttons" class="form-actions">
        <%= button Spree.t(:search), 'search.svg' %>
      </div>
    <% end %>

  </div>
<% end %>

<% if @collection.any? %>
<div class="table-responsive">
  <table class="table" id="listing_products">
    <thead>
      <tr data-hook="admin_products_index_headers">
        <th scope="col" colspan="2"><%= sort_link @search,:name, Spree.t(:name), { default_order: "desc" }, {title: 'admin_products_listing_name_title'} %></th>
        <th scope="col"><%= Spree.t(:sku) %></th>
        <th scope="col"><%= Spree.t(:status) %></th>
        <th scope="col" class="text-center">
          <%= sort_link @search, :master_default_price_amount, Spree.t(:master_price), {}, {title: 'admin_products_listing_price_title'} %>
        </th>
        <th scope="col" data-hook="admin_products_index_header_actions" class="actions"></th>
      </tr>
    </thead>
    <tbody>
      <% @collection.each do |product| %>
        <% available_status = available_status(product) %>
          <tr <%== "style='color: red;'" if product.deleted? %> id="<%= spree_dom_id product %>" data-hook="admin_products_index_rows" class="<%= cycle('odd', 'even') %>">
            <td scope="row" class="image">
              <%= link_to edit_admin_product_path(product) do %>
                <%= mini_image product %>
              <% end %>
            </td>
            <td><%= link_to product.try(:name), edit_admin_product_path(product) %></td>
            <td class="sku"><%= product.sku rescue '' %></td>
            <td class="status"><%= active_badge(available_status == Spree.t(:available), label: available_status) %></td>
            <td class="text-center"><%= display_price(product) %></td>
            <td class="actions " data-hook="admin_products_index_row_actions">
              <span class="d-flex justify-content-end">
                <%= link_to_edit product, no_text: true, class: 'edit' if can?(:edit, product) && !product.deleted? %>
                <%= link_to_clone product, no_text: true, class: 'clone' if can?(:clone, product) %>
                <%= link_to_delete product, no_text: true if can?(:delete, product) && !product.deleted? %>
              </span>
            </td>
          </tr>
      <% end %>
    </tbody>
  </table>
</div>
<% else %>
  <div class="alert alert-info no-objects-found">
    <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Product)) %>,
    <%= link_to Spree.t(:add_one), new_object_url if can?(:create, Spree::Product) %>!
  </div>
<% end %>

<%= render partial: 'spree/admin/shared/index_table_options', locals: { collection: @collection } %>
